import java.util.concurrent.PriorityBlockingQueue;

/*class Mytask implements Comparable<Mytask>
{
    public Runnable runnable;
    public long time;

    public Mytask(Runnable runnable, long time) {
        this.runnable = runnable;
        this.time = time;
    }
    public long time()
    {
        return this.time;
    }

    @Override
    public int compareTo(Mytask o) {
        return  (int)(this.time-o.time);
    }
    public void getrun()
    {
        runnable.run();
    }
}


class timer
{
    public Thread t=null;

    PriorityBlockingQueue<Mytask> queue=new PriorityBlockingQueue<>();
    public timer() {
        t = new Thread(() -> {
            while (true) {
                try {
                    // 取出队首元素, 检查看看队首元素任务是否到时间了.
                    // 如果时间没到, 就把任务塞回队列里去.
                    // 如果时间到了, 就把任务进行执行.
                    synchronized (this) {
                        Mytask mytask = queue.take();
                        long curTime = System.currentTimeMillis();
                        if (curTime < mytask.time()) {
                            // 还没到点, 先不必执行
                            // 现在是 13:00, 取出来的任务是 14:00 执行
                            queue.put(mytask);
                            // 在 put 之后, 进行一个 wait
                            this.wait(mytask.time() - curTime);
                        } else {
                            // 时间到了!! 执行任务!!
                            mytask.getrun();
                        }
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t.start();
    }




    public void schedule(Runnable runnable,long time)
    {
        long time1=System.currentTimeMillis();
        Mytask mytask=new Mytask(runnable,time+time1);
        queue.put(mytask);
        synchronized (this)
        {
            this.notify();
        }



    }
}





public class Mytimer {
    public static void main(String[] args) {
        timer mytimer=new timer();
        mytimer.schedule(new Runnable(){
            public void run()
            {
                System.out.println("任务1");
            }


        },1000);
        mytimer.schedule(new Runnable(){
            public void run()
            {
                System.out.println("任务2");
            }

        },2000);
    }


}

 */


